﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BfLib.Text
{
    public class WordCycler2
    {
        private const string _slower = "abcdefghijklmnopqrstuvwxyz";
        private const string _supper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private const string _sspecial = " ,.<>|;:{}[]!@#$%^&*()-=_+~"; //@" ' `"; // omitted chars \ and "
        private const string _snumbers = "0123456789";
        private string thechars = string.Empty;
        private char _lastchar;
        int _i = 0;

        public WordCycler2(bool lower, bool upper, bool special, bool numbers)
        {
            StringBuilder sb = new StringBuilder();
            if (lower)
            {
                sb.Append(_slower);
            }
            if (upper)
            {
                sb.Append(_supper);
            }
            if (special)
            {
                sb.Append(_sspecial);
            }
            if (numbers)
            {
                sb.Append(_snumbers);
            }
            thechars = sb.ToString();
        }

        public string Increment(string s, out bool incremented)
        {
            _lastchar = s[s.Length - 1];
            _i = thechars.IndexOf(_lastchar);

            if ((_i >= 0) && (_i <= thechars.Length - 2))
            {
                _lastchar = thechars[_i + 1];
                s = string.Format("{0}{1}", s.Substring(0, s.Length - 1), _lastchar);
                incremented = true;
            }
            else
            {
                incremented = false;
            }

            return s;
        }
        
        private string ResetCharacter(string s, int charIndex)
        {
            char[] ss = s.ToArray();
            ss[charIndex] = thechars[charIndex];

            for (int i = charIndex + 1; i < ss.Length; i++)
            {
                ss[i] = thechars[0];
            }

            return new string(ss);
        }
    }
}
